﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JMT.Data
{
    public abstract class JContainer : JModel, IJContainer
    {
        private JCollection _rootContainer;

        private List<string> _childrenTypes;

        public JContainer(string type)
            : base(type)
        {
            _childrenTypes = new List<string>();
            OnRegisterChildrenType(ref _childrenTypes);

            JCollection curContainer = null;
            foreach (var item in _childrenTypes)
            {
                if (_rootContainer == null)
                {
                    _rootContainer = new JCollection(item);
                    curContainer = _rootContainer;
                }
                else
                {
                    JCollection container = new JCollection(item);
                    curContainer = curContainer.Link(container);
                }
            }
        }

        /// <summary>
        /// 注册可以包含的子类型
        /// </summary>
        /// <param name="childrenTypes"></param>
        protected abstract void OnRegisterChildrenType(ref List<string> childrenTypes);

        public JModel GetModel(Guid id)
        {
            return _rootContainer.Get(id);
        }

        public void Add(JModel jModel)
        {
            _rootContainer.Add(jModel);
        }

        public void Remove(JModel jModel)
        {
            _rootContainer.Remove(jModel);
        }

        public JModel Get(Guid id)
        {
            return _rootContainer.Get(id);
        }

        public List<JModel> ToList()
        {
            return _rootContainer.ToList();
        }

        public List<T> GetChildren<T>() where T : JModel
        {
            return _rootContainer.GetChildren<T>();
        }
    }
}
